package com.ironman.analyze.core.compute;

import com.ironman.analyze.core.compute.base.BaseComputer;
import com.ironman.analyze.core.compute.base.BaseEmptyComputer;
import com.ironman.analyze.core.compute.entity.GpsWindowData;
import com.ironman.analyze.core.context.TrainingContext;
import com.ironman.analyze.core.handler.GpsPostHandler;
import com.ironman.common.entity.GpsData;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * gps计算
 */
@Slf4j
public class GpsEmptyComputer extends BaseEmptyComputer<GpsData> implements BaseComputer<GpsData, GpsWindowData> {

    private final GpsPostHandler gpsPostHandler;

    public GpsEmptyComputer(TrainingContext trainingContext) {
        super(trainingContext);
        this.gpsPostHandler = new GpsPostHandler();
    }

    @Override
    public void computeWindowData(List<GpsData> dataList) {
        for (GpsData gpsData : dataList) {
            windowData(gpsData, new BiConsumer<Date, GpsData>() {
                @Override
                public void accept(Date ts, GpsData gpsData) {
                    gpsPostHandler.handler(new GpsWindowData(ts, gpsData.getLongitude(), gpsData.getLatitude()), new Consumer<GpsWindowData>() {
                        @Override
                        public void accept(GpsWindowData gpsWindowData) {
                            syncData(gpsWindowData);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void syncData(GpsWindowData windowData) {
        getTrainingContext().getGpsWindowDataMap().put(windowData.getTs(), windowData);
    }
}
